﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmPackingQuantityManage : Form
    {
        UserInfo ui;

        SqlTools sqt;

        string strSelectInfoString = "select distinct @字段  collate Chinese_PRC_CS_AI from @表 order by @字段 collate Chinese_PRC_CS_AI";
        string strSelectPackingQuantityString = "select 序号,质量等级,型号,尺寸,编带,包装数量,增加人,增加时间 from 产品尺寸与包装数量表";
        string strDeleteString = "delete from 产品尺寸与包装数量表 where 序号=@序号";
        string strCheckExist = "select 序号 from 产品尺寸与包装数量表 where 质量等级='@质量等级' and 型号='@型号' and 尺寸='@尺寸' and 编带='@编带'";
        string strInsertString = "insert into 产品尺寸与包装数量表 (质量等级,型号,尺寸,编带,包装数量,增加人) values ('@质量等级','@型号','@尺寸','@编带',@包装数量,'@增加人')";

        public frmPackingQuantityManage(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
        }

        private void frmPackingQuantityManage_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            iniComboBox();
        }

        private void iniComboBox()
        {
            //保存已选择索引
            int idxClass = cmbClass.SelectedIndex;
            int idxModel = cmbModel.SelectedIndex;
            int idxSize = cmbSize.SelectedIndex;
            int idxClass2 = cmbClass2.SelectedIndex;
            int idxModel2 = cmbModel2.SelectedIndex;
            int idxSize2 = cmbSize2.SelectedIndex;
            //填充质量等级列表
            sqt.FillComboBox(cmbClass, strSelectInfoString.Replace("@字段", "质量等级")
                                                                                         .Replace ("@表","QZKFC"));
            cmbClass.Items.Remove("[NULL]");
            cmbClass.Items.Insert(0, "忽略等级");
            //填充型号列表
            sqt.FillComboBox(cmbModel, strSelectInfoString.Replace("@字段", "型号")
                                                                                        .Replace("@表", "QZKFC"));
            cmbModel.Items.Remove("[NULL]");
            cmbModel.Items.Insert(0, "忽略型号");
            //填充尺寸列表
            sqt.FillComboBox(cmbSize, strSelectInfoString.Replace("@字段", "尺寸")
                                                                                        .Replace("@表", "QZKFC"));
            cmbSize.Items.Remove("[NULL]");
            cmbSize.Items.Insert(0, "忽略尺寸");
            //填充查询用质量等级列表
            sqt.FillComboBox(cmbClass2, strSelectInfoString.Replace("@字段", "质量等级")
                                                                                         .Replace("@表", "产品尺寸与包装数量表"));
            cmbClass2.Items.Insert(0, "全部");
            //填充查询用型号列表
            sqt.FillComboBox(cmbModel2, strSelectInfoString.Replace("@字段", "型号")
                                                                                        .Replace("@表", "产品尺寸与包装数量表"));
            cmbModel2.Items.Insert(0, "全部");
            //填充查询用尺寸列表
            sqt.FillComboBox(cmbSize2, strSelectInfoString.Replace("@字段", "尺寸")
                                                                                        .Replace("@表", "产品尺寸与包装数量表"));
            cmbSize2.Items.Insert(0, "全部");
            //恢复索引
            if (idxClass < 0&&cmbClass.Items.Count >0)
            {
                idxClass = 0;
            }
            cmbClass.SelectedIndex=idxClass ;
            if (idxModel < 0 && cmbModel.Items.Count > 0)
            {
                idxModel = 0;
            }
            cmbModel.SelectedIndex=idxModel;
            if (idxSize < 0 && cmbSize.Items.Count > 0)
            {
                idxSize = 0;
            }
            cmbSize.SelectedIndex=idxSize;
            if (idxClass2 < 0 && cmbClass2.Items.Count > 0)
            {
                idxClass2 = 0;
            }
            cmbClass2.SelectedIndex=idxClass2;
            if (idxModel2 < 0 && cmbModel2.Items.Count > 0)
            {
                idxModel2 = 0;
            }
            cmbModel2.SelectedIndex=idxModel2;
            if (idxSize2 < 0 && cmbSize2.Items.Count > 0)
            {
                idxSize2 = 0;
            }
            cmbSize2.SelectedIndex=idxSize2;
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strTmpSelectString = strSelectPackingQuantityString;
            if (!cmbClass2.Text.Equals("全部"))
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                }
                strTmpSelectString += " 质量等级='" + cmbClass2.Text + "'";
            }
            if (!cmbModel2.Text.Equals("全部"))
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                }
                strTmpSelectString += " 型号='" + cmbModel2.Text + "'";
            }
            if (!cmbSize2.Text.Equals("全部"))
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                }
                strTmpSelectString += " 尺寸='" + cmbSize2.Text + "'";
            }
            if (strTmpSelectString.Contains(" where "))
            {
                strTmpSelectString += " and ";
            }
            else
            {
                strTmpSelectString += " where ";
            }
            if (chkBD2.Checked)
            {
                strTmpSelectString += " 编带=1";
            }
            else
            {
                strTmpSelectString += " 编带=0";
            }
            strTmpSelectString += " order by 编带,质量等级,型号,尺寸";
            DataTable tmpdt = sqt.GetData(strTmpSelectString);
            dgv1 .DataSource =tmpdt ;
            dgv1 .AutoResizeColumns ();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1 .CurrentRow !=null )
            {
                if (MessageBox.Show("确定要删除序号为" + GeneralTools .GetString (dgv1 .CurrentRow .Cells ["序号"].Value ,string.Empty ) + "的设置？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery(strDeleteString.Replace("@序号", GeneralTools.GetString(dgv1.CurrentRow.Cells["序号"].Value, string.Empty)));
                    iniComboBox();
                    ComboBox_SelectedIndexChanged(this, new EventArgs());
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cmbClass.Text.Trim () .Equals (string.Empty ))
            {
                GeneralTools.ShowInfoMsg("请选择质量等级！");
                cmbClass.Focus();
                return;
            }
            if (cmbModel.Text.Trim().Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("请选择型号！");
                cmbModel.Focus();
                return;
            }
            if (cmbSize.Text.Trim().Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("请选择尺寸！");
                cmbSize.Focus();
                return;
            }
            if (txtQuantity.Text.Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("请填写包装数量！");
                txtQuantity.Focus();
                return;
            }
            if (sqt.RecordExist(strCheckExist.Replace("@质量等级", cmbClass.Text)
                                                                  .Replace("@型号", cmbModel.Text)
                                                                  .Replace("@尺寸", cmbSize.Text)
                                                                  .Replace ("@编带",GeneralTools .GetString (chkBD .Checked,string.Empty ) )))
            {
                GeneralTools.ShowWarningMsg("该设置已存在，请检查后重新输入！");
                return;
            }
            if (sqt.DoCmdExecuteNonQuery(strInsertString.Replace("@质量等级", cmbClass.Text)
                                                                                          .Replace("@型号", cmbModel.Text)
                                                                                          .Replace("@尺寸", cmbSize.Text)
                                                                                          .Replace ("@编带",GeneralTools .GetString (chkBD .Checked,string.Empty ))
                                                                                          .Replace("@包装数量", txtQuantity.Text)
                                                                                        .Replace("@增加人", ui.UserName)) == 1)
            {
                GeneralTools.ShowInfoMsg("记录增加成功！");
                ComboBox_SelectedIndexChanged(this, new EventArgs());
                iniComboBox();
            }
            else
            {
                GeneralTools.ShowErrorMsg("记录增加失败！"+sqt.ErrMessage );
            }
        }

        private void txtQuantity_Leave(object sender, EventArgs e)
        {
            txtQuantity.Text =GeneralTools .GetString (GeneralTools.GetInt(txtQuantity.Text, -1),string.Empty );
            if (txtQuantity .Text .Equals ("-1"))
            {
                txtQuantity.Text = string.Empty;
            }
        }

        private void chkBD2_CheckedChanged(object sender, EventArgs e)
        {
            ComboBox_SelectedIndexChanged(this, new EventArgs());
        }

        private void btnRefreshTree_Click(object sender, EventArgs e)
        {
            tv.Nodes.Clear();
            #region 生成根节点
            //获取根节点内容-质量等级
            DataTable tmpdt = sqt.GetData("select distinct 质量等级  collate Chinese_PRC_CS_AI 等级 from 产品尺寸与包装数量表 where 编带='" + GeneralTools.GetString(chkBD2.Checked, string.Empty) + "' order by 质量等级 collate Chinese_PRC_CS_AI");
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i<tmpdt.Rows.Count; i++)
                {
                    TreeNode tn_zldj = new TreeNode(GeneralTools.GetString(tmpdt.Rows[i]["等级"], string.Empty));
                    //获取次节点内容-型号
                    DataTable tmpdt_xh = sqt.GetData("select distinct 型号  collate Chinese_PRC_CS_AI 型号规格 from 产品尺寸与包装数量表 where 质量等级='" + GeneralTools.GetString(tmpdt.Rows[i]["等级"], string.Empty) + "' and 编带='" + GeneralTools.GetString(chkBD2.Checked, string.Empty) + "' order by 型号 collate Chinese_PRC_CS_AI");
                    if (tmpdt_xh.Rows.Count > 0)
                    {
                        for (int j = 0; j < tmpdt_xh.Rows.Count; j++)
                        {
                            TreeNode tn_xh = new TreeNode(GeneralTools.GetString(tmpdt_xh.Rows[j]["型号规格"], string.Empty));
                            //获取末节点内容-尺寸：包装数量
                            DataTable tmpdt_cc = sqt.GetData("select distinct (尺寸+':'+cast(包装数量 as varchar(10)))  collate Chinese_PRC_CS_AI from 产品尺寸与包装数量表 where 质量等级='" + GeneralTools.GetString(tmpdt.Rows[i]["等级"], string.Empty) + "' and 型号='" + GeneralTools.GetString(tmpdt_xh.Rows[j]["型号规格"], string.Empty) + "' and 编带='" + GeneralTools.GetString(chkBD2.Checked, string.Empty) + "' order by (尺寸+':'+cast(包装数量 as varchar(10))) collate Chinese_PRC_CS_AI");
                            if (tmpdt_cc.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt_cc.Rows.Count; k++)
                                {
                                    TreeNode tn_cc = new TreeNode(GeneralTools.GetString(tmpdt_cc.Rows[k][0], string.Empty));
                                    tn_xh.Nodes.Add(tn_cc);
                                }
                            }
                            tn_zldj.Nodes.Add(tn_xh);
                        }
                    }
                    tv.Nodes.Add(tn_zldj);
                }
            }
            #endregion

        }
    }
}